@echo off
chcp 65001 >nul

echo ======================================
echo      IAS应用平台启动脚本
echo ======================================

:: 检查Docker是否安装
docker --version >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker 未安装，请先安装 Docker
    pause
    exit /b 1
)

:: 检查Docker Compose是否安装
docker-compose --version >nul 2>&1
if errorlevel 1 (
    echo 错误: Docker Compose 未安装，请先安装 Docker Compose
    pause
    exit /b 1
)

:: 创建必要的目录
echo 创建必要的目录...
if not exist "docker\rocketmq" mkdir docker\rocketmq
if not exist "logs" mkdir logs

:: 创建RocketMQ Broker配置文件
echo 创建RocketMQ配置文件...
(
echo brokerClusterName = DefaultCluster
echo brokerName = broker-a
echo brokerId = 0
echo deleteWhen = 04
echo fileReservedTime = 48
echo brokerRole = ASYNC_MASTER
echo flushDiskType = ASYNC_FLUSH
echo brokerIP1 = rocketmq-broker
) > docker\rocketmq\broker.conf

:: 编译项目
echo 编译项目...
call mvn clean package -DskipTests

if errorlevel 1 (
    echo 错误: 项目编译失败
    pause
    exit /b 1
)

:: 启动基础设施
echo 启动基础设施...
docker-compose up -d mysql redis nacos rocketmq-nameserver rocketmq-broker rocketmq-console

:: 等待基础设施启动完成
echo 等待基础设施启动完成...
timeout /t 30 /nobreak >nul

:: 检查Nacos是否启动成功
echo 检查Nacos服务状态...
set /a count=0
:check_nacos
set /a count+=1
curl -f http://localhost:8848/nacos/v1/console/health/readiness >nul 2>&1
if not errorlevel 1 (
    echo Nacos启动成功
    goto start_services
)
echo 等待Nacos启动... (%count%/30)
timeout /t 5 /nobreak >nul
if %count% lss 30 goto check_nacos

:start_services
:: 启动应用服务
echo 启动应用服务...
docker-compose up -d api-gateway user-service

echo ======================================
echo 系统启动完成！
echo ======================================
echo 服务访问地址：
echo - API网关: http://localhost:8080
echo - Nacos控制台: http://localhost:8848/nacos
echo - RocketMQ控制台: http://localhost:8076
echo - 用户服务: http://localhost:8001
echo.
echo 默认账号：
echo - 用户名: admin
echo - 密码: 123456
echo ======================================
pause 